package leetcode.editor.week.week315;

import java.util.HashSet;
import java.util.Set;

// 6205. 反转之后不同整数的数目
// https://leetcode.cn/problems/count-number-of-distinct-integers-after-reverse-operations/
public class Solution6205 {
    public int countDistinctIntegers(int[] nums) {
        int n = nums.length;
        int[] arr = new int[n + n];
        for (int i = 0; i < n; i++) {
            arr[i] = nums[i];
        }

        int j = 0;
        for (int i = n; i < 2 * n; i++) {
            arr[i] = reverse(nums[j]);
            j++;
        }

        Set<Integer> set = new HashSet<>();
        int ans = 0;
        for (int i = 0; i < 2 * n; i++) {
            if (set.contains(arr[i])) {
                continue;
            } else {
                set.add(arr[i]);
                ans++;
            }
        }

        return ans;
    }

    public int reverse(int n) {
        String s = rev(String.valueOf(n));
        return Integer.parseInt(s);
    }

    public String rev(String s) {
        StringBuilder sb = new StringBuilder(s);
        int n = s.length();
        int i = 0, j = n - 1;
        while (i <= j) {
            char c = sb.charAt(i);
            sb.setCharAt(i, sb.charAt(j));
            sb.setCharAt(j, c);
            i++;
            j--;
        }

        return sb.toString();
    }
}
